package com.uhylab.uhuvinetest.qrcamera.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by xyb on 16/1/10 at UHylab
 *
 * @version 1.0
 */
public class CreateBitmapThread extends Thread {
    private String pathString;
    private ImageView mResultImage;
    private Context context;
    private Handler handler;
    private float[] floats;
    private int[] weidongs;
    private int[] liumings;
    private float left_x;
    private float right_x;
    private float top_y;
    private float bottom_y;
    private int[] types = new int[11];

    static {
        System.loadLibrary("JniOp");
    }

    public CreateBitmapThread(Handler handler, Context context, String pathString, ImageView mResultImage, float[] floats, int[] weidongs, int[] liumings) {
        this.pathString = pathString;
        this.mResultImage = mResultImage;
        this.context = context;
        this.handler = handler;
        this.floats = floats;
        this.weidongs = weidongs;
        this.liumings = liumings;
    }

    @Override
    public void run() {
        super.run();

         Bitmap bitmap =
                getDiskBitmap(pathString);
        Message btMessage = new Message();
        btMessage.what = 2;
        btMessage.obj = bitmap;
        handler.sendMessage(btMessage);

        ArrayList<Byte> colorsList = new ArrayList();
        ArrayList<Integer> normRGBList = new ArrayList();

        // 获取图片块的 平均rgb值
        initBitmap(bitmap, colorsList, normRGBList);
        for (Integer integer : normRGBList) {
            Log.e("CreateBitmapThread", "integer:" + integer);
        }

        byte[] colorsBytes = new byte[colorsList.size()];
        ///**返回的位置为正许的**/
        for (int i = 0; i < colorsBytes.length; i++) {
            colorsBytes[i] = colorsList.get(i);
        }

        if (colorsList != null) {
            colorsList.clear();
        }

        /**-------                         ------**/

        float[] colorFloats = GetColor(colorsBytes, bitmap.getWidth(), bitmap.getHeight(), colorsBytes.length);
//        for (int type : types) {
//            Log.e("CreateBitmapThread", "typ1:" + type);
//        }
        if (colorFloats.length != colorsBytes.length) {
            mResultImage.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context, "分析失败,请重试", Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            Message message = new Message();
            Bundle bundle = new Bundle();
            bundle.putFloatArray("list", colorFloats);
            bundle.putIntArray("types", types);
            bundle.putIntArray("weidongs", weidongs);
            bundle.putIntArray("liumings", liumings);
            message.obj = bundle;
            message.what = 0;
            handler.sendMessage(message);
        }


    }

    /**
     *
     * */
    private void initBitmap(Bitmap bitmap, ArrayList<Byte> arrayList, ArrayList<Integer> normRGBList) {

        for (int i = 0; i < floats.length / 4; i++) {
            left_x = floats[i * 4];
            right_x = floats[i * 4 + 2];
            left_x = ((right_x - left_x) / 3) + left_x;
            right_x = right_x - ((right_x - left_x) / 3);
            ArrayList<Integer> r_center = new ArrayList();
            ArrayList<Integer> g_center = new ArrayList();
            ArrayList<Integer> b_center = new ArrayList();

            for (float x = left_x; x < right_x; x++) {
                top_y = floats[i * 4 + 1];
                bottom_y = floats[i * 4 + 3];
                top_y = ((bottom_y - top_y) / 3) + top_y;
                bottom_y = bottom_y - ((bottom_y - top_y) / 3);
                for (float y = top_y; y < bottom_y; y++) {
                    int pixel = bitmap.getPixel((int) x, (int) y);
                    // 下面三行代码将一个数字转换为RGB数字
                    int red = ((pixel & 0xff0000) >> 16);
                    int green = ((pixel & 0xff00) >> 8);
                    int blue = (pixel & 0xff);
                    if (!(red < 15 && green < 15 && blue < 15)) {
                        r_center.add(red);
                        g_center.add(green);
                        b_center.add(blue);
                    }

                }
            }
            int redMax = 0;

            for (int red = 0; red < r_center.size(); red++) {
                redMax += r_center.get(red);
            }
            redMax = redMax / r_center.size();

            int greenMax = 0;

            for (int green = 0; green < r_center.size(); green++) {
                greenMax += g_center.get(green);
            }

            greenMax = greenMax / g_center.size();
            int blueMax = 0;

            for (int blue = 0; blue < r_center.size(); blue++) {
                blueMax += b_center.get(blue);
            }
            blueMax = blueMax / b_center.size();
            Log.e("CreateBitmapThread", "redMax:" + redMax + "," + greenMax + "," + blueMax);

            if (i <= 10) {
                arrayList.add((byte) redMax);
                arrayList.add((byte) greenMax);
                arrayList.add((byte) blueMax);
            } else {
                normRGBList.add(redMax);
                normRGBList.add(greenMax);
                normRGBList.add(blueMax);
            }

        }
    }


    private Bitmap getDiskBitmap(String pathString) {
        Bitmap bitmap = null;
        try {
            File file = new File(pathString);
            if (file.exists()) {
                bitmap = BitmapFactory.decodeFile(pathString);
                Matrix matrix = new Matrix();
                matrix.setRotate(90);
                int bitmapWidth = bitmap.getWidth();
                int bitmapHeight = bitmap.getHeight();
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);
            }
        } catch (Exception e) {
            mResultImage.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context, "检测失败请重试", Toast.LENGTH_SHORT).show();
                }
            });
        }
        return bitmap;
    }

    public native float[] GetColor(byte[] bytes, int i, int i1, int i2);


}
